package com.jec.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

public class EncryptUtils {

	public static String encriptedKey="jec";

	public static String encryptPlainPassword(String password){
		try {
			return encryptMd5Password(getMD5(password));
		}catch (NoSuchAlgorithmException e){
			e.printStackTrace();
			return "";
		}
	}

	public static String encryptMd5Password(String password) {
		if (StringUtils.length(password) != 32) {
			throw new IllegalArgumentException("MD5 string required!");
		}

		String salt = StringUtils.substring(password, 27);
		return DigestUtils.md5Hex(password + salt);
	}

	public static String getMD5(String str) throws NoSuchAlgorithmException{
		// 生成一个MD5加密计算摘要
		MessageDigest md = MessageDigest.getInstance("MD5");
		// 计算md5函数
		md.update(str.getBytes());
		// digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
		// BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
		return new BigInteger(1, md.digest()).toString(16);
	}
}
